package com.sly.slf4jmdc.interceptor;

import com.sly.slf4jmdc.common.util.TraceContext;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.MDC;
import org.springframework.stereotype.Component;

/**
 * @author SLY
 * @date 2022/3/11
 */
@Slf4j
@Component
public class TraceIdInterceptor implements RequestInterceptor {
    @Override
    public void apply(RequestTemplate requestTemplate) {
        String traceId = MDC.get(TraceContext.TRACE_ID);
        if (StringUtils.isNotBlank(traceId)) {
            requestTemplate.header(TraceContext.TRACE_ID, traceId);
        }
    }
}
